package sseGameApps.service.mdkbr;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slim3.datastore.Datastore;
import org.slim3.util.BeanUtil;

import sseGameApps.meta.mdkbr.MdKbrUserMeta;
import sseGameApps.model.mdkbr.MdKbrUser;

import com.google.appengine.api.datastore.Transaction;


public class MdKbrUserService {

    private MdKbrUserMeta e = new MdKbrUserMeta();

    public MdKbrUser registPlayerInfo(Map<String, Object> input) {

        MdKbrUser data = Datastore
                .query(e)
                .filter(e.userId.equal(input.get("userId").toString()))
                .asSingle();

        if (data != null) {
            BeanUtil.copy(input, data);

            Transaction tx = Datastore.beginTransaction();
            Datastore.put(data);
            tx.commit();

            return data;

        } else {
            return null;
        }

    }

    public Integer checkUserLevel(Long userExp) {

        int userLevel = 1;
        List<Long> arrayLevel = getLevelArray();

        for (int i = 0; i < arrayLevel.size(); i++){
            if(arrayLevel.get(i) <= userExp){
                userLevel++;
            } else {
                break;
            }
        }

        return userLevel;

    }

    public Long checkUserNextLevelExp(Long userExp) {

        List<Long> arrayLevel = getLevelArray();

        for (int i = 0; i < arrayLevel.size(); i++){
            if(arrayLevel.get(i) >= userExp){
                return arrayLevel.get(i) - userExp;
            }
        }

        return null;

    }

    private List<Long> getLevelArray(){

        List<Long> arrayLevel = new ArrayList<Long>();

        arrayLevel.add(Long.parseLong("100"));  //Lv2
        arrayLevel.add(Long.parseLong("500"));  //Lv3
        arrayLevel.add(Long.parseLong("1500")); //Lv4
        arrayLevel.add(Long.parseLong("5000")); //Lv5
        arrayLevel.add(Long.parseLong("10000")); //Lv6
        arrayLevel.add(Long.parseLong("20000")); //Lv7
        arrayLevel.add(Long.parseLong("50000"));  //Lv8
        arrayLevel.add(Long.parseLong("100000"));  //Lv9
        arrayLevel.add(Long.parseLong("300000"));  //Lv10
        arrayLevel.add(Long.parseLong("500000"));  //Lv11
        arrayLevel.add(Long.parseLong("700000"));  //Lv12
        arrayLevel.add(Long.parseLong("900000"));  //Lv13
        arrayLevel.add(Long.parseLong("110000"));  //Lv14
        arrayLevel.add(Long.parseLong("130000"));  //Lv15
        arrayLevel.add(Long.parseLong("150000"));  //Lv16
        arrayLevel.add(Long.parseLong("170000"));  //Lv17
        arrayLevel.add(Long.parseLong("190000"));  //Lv18
        arrayLevel.add(Long.parseLong("210000"));  //Lv19
        arrayLevel.add(Long.parseLong("230000"));  //Lv20

        return arrayLevel;

    }

    public List<MdKbrUser> getEnemyList(Integer level) {

        return Datastore
                .query(e)
                .filter(e.userLevel.greaterThanOrEqual(level))
                .sort(e.userLevel.asc,e.updatedAt.asc)
                .limit(10)
                .asList();

    }

    public List<MdKbrUser> getMdKbrUserWinBest10() {

        return Datastore
                .query(e)
                .sort(e.winCount.desc, e.loseCount.asc, e.battleCount.desc)
                .limit(10)
                .asList();

    }
    public boolean checkNoSamePlayer(String playerName) {

        MdKbrUser data = Datastore
                .query(e)
                .filter(e.playerName.equal(playerName))
                .limit(1)
                .asSingle();

        if (data == null){
            return true;
        }

        return false;

    }

    public MdKbrUser getMdKbrUser(String userId) {

        return Datastore
                .query(e)
                .filter(e.userId.equal(userId))
                .asSingle();

    }

    public void putMdKbrUser(MdKbrUser data) {

        Transaction tx = Datastore.beginTransaction();
        Datastore.put(data);
        tx.commit();

    }

}
